//
//  StoryViewController.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/17.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "StoryViewController.h"
#import "LBAFNetworking.h"
#import "LuBDefine.h"
#import "StoryTableViewCell.h"
#import "Story.h"
#import "AlbumViewController.h"
#import "NoneView.h"
#import <MJRefresh.h>
#import "LuBinCollectionViewController.h"

@interface StoryViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *storyArray;

@property (nonatomic, assign)NSInteger size;

@property (nonatomic, assign)BOOL isUpLoading;

@end

@implementation StoryViewController

- (void)dealloc{
    [_pid release];
    [_tableView release];
    [_storyArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.storyArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsDefaultPrompt];
    [self createStoryViewController];
    // Do any additional setup after loading the view.
}

- (void)downloadData:(NSInteger)size{
    NSString *url = [NSString stringWithFormat:@"http://api.epetbar.com/gutouv2/pet.html?do=photolinenew&month=0&limit=2&year=0&type=2&page=%ld&timeline=1&pid=%@&develop=0",self.size,self.pid];
    [LBNetWork handleGETWithUrlString:url parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        if (!self.isUpLoading) {
            [self.storyArray removeAllObjects];
        }
        NSArray *array = [[responseObject objectForKey:@"data"] objectForKey:@"list"];
        
        for (NSDictionary *dic in array) {
            if ([[dic objectForKey:@"list"] count] != 0) {
                NSDictionary *tempDic =  [[dic objectForKey:@"list"] firstObject];
                Story *story = [Story storyWithDictionary:tempDic];
                [self.storyArray addObject:story];
            }
        }
        if (self.storyArray.count == 0) {
            NoneView *noneView = [[NoneView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, CHEIGHT)];
            [self.view addSubview:noneView];
            [noneView release];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        // 没有更多数据
        if (array.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (void)createStoryViewController{
    
    self.navigationItem.title = @"图集";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 0, CWIDTH - 20, CHEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    [visualEffectView setFrame:CGRectMake(0, 0, CWIDTH, 64)];
    [self.view addSubview:visualEffectView];
    [visualEffectView release];
    [self addHeaderRefresh];
    [self addFooterUploading];
}

- (void) addHeaderRefresh{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.size = 1;
        self.isUpLoading = NO;
        [self downloadData:self.size];
    }];
    [self.tableView.header beginRefreshing];
}

- (void) addFooterUploading{
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        self.size += 1 + 1;
        self.isUpLoading = YES;
        [self downloadData:self.size];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.storyArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    StoryTableViewCell *storyTableViewCell = [StoryTableViewCell storyTableCellWithTableView:tableView];
    storyTableViewCell.layer.masksToBounds = YES;
    storyTableViewCell.layer.cornerRadius = 10;
    storyTableViewCell.story = _storyArray[indexPath.section];
    return storyTableViewCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (CHEIGHT == 480) {
        return CHEIGHT / 2.1;
    }if (CHEIGHT == 568) {
        return  CHEIGHT / 2.3;
    }else{
        return CHEIGHT / 2.45;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AlbumViewController *albumViewController = [[AlbumViewController alloc]init];
    albumViewController.ppid = [_storyArray[indexPath.section] ppid];
    albumViewController.photo_des = [_storyArray[indexPath.section] photo_des];
    [self.navigationController pushViewController:albumViewController animated:YES];    
    [albumViewController release];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
